﻿Imports VendasBLL
Imports VendasDAL
Public Class UsuarioFRM
    Dim ObjBLL As New UsuarioBLL
    Public Op As String
    Public Codfuncionario As Integer
    Public Codigo As Integer
#Region "Carrega Campos"
    Sub CarregaCampos()
        ObjBLL = New UsuarioBLL(codigo)
        LBCodigo.Text = "Código: " & ObjBLL.Codigo
        LBTitulo.Text = ObjBLL.Descricao
        TXTLogin.Text = ObjBLL.Descricao
        TXTFuncionario.Text = ObjBLL.funcionario
        Codfuncionario = ObjBLL.codfuncionario
        CBStatus.SelectedValue = ObjBLL.Status
    End Sub
#End Region

#Region "Limpar campos"
    Sub Limpa()
        CBStatus.SelectedIndex = 0
        TXTLogin.Text = ""
        Codfuncionario = 0
        TXTFuncionario.Text = ""
        CKBSenha.Checked = False
        Me.ObjBLL = Nothing
    End Sub
#End Region

#Region "Load"
    Private Sub UsuarioFRM_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        objconexao.carregaCombo(CBStatus, "Generico.Gen_Status", "sta_codigo", "sta_descricao", "sta_codigo < 3", "sta_codigo")
        Limpa()
        If (Op = "I") Then
            LBCodigo.Visible = False
            LBTitulo.Text = "Usuário"
            LBCodigo.Text = 0
            CKBSenha.Enabled = False
        Else
            LBCodigo.Visible = True
            CKBSenha.Enabled = False
            CKBSenha.Enabled = True
            CarregaCampos()
        End If
    End Sub
#End Region

#Region "Salvar/Alterar"
    Private Sub TSBSalvar_Click(sender As System.Object, e As System.EventArgs) Handles TSBSalvar.Click

        Try

            ObjBLL = New UsuarioBLL(Codigo, Trim(TXTLogin.Text), CBStatus.SelectedValue, Codfuncionario, CKBSenha.Checked)
            If Op = "I" Then
                ObjBLL.Inserir()
                MessageBox.Show("Usuário inserido com sucesso", "Sucesso", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Limpa()
                Me.Close()
            ElseIf Op = "A" Then
                ObjBLL.Alterar()
                MessageBox.Show("Usuário alterada com sucesso", "Sucesso", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Limpa()
                Me.Close()
            Else

            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try
    End Sub
#End Region

#Region "Voltar"
    Private Sub TSBVoltar_Click(sender As System.Object, e As System.EventArgs) Handles TSBVoltar.Click
        Me.Close()
    End Sub
#End Region


    Private Sub BBuscar_Click(sender As System.Object, e As System.EventArgs) Handles BBuscar.Click
        ConsultaFuncionariosFRM.Tipo = "ret"
        ConsultaFuncionariosFRM.ShowDialog()
    End Sub


    Private Sub BSenha_Click(sender As System.Object, e As System.EventArgs)
        TSBSalvar_Click(sender, e)
    End Sub

    Private Sub UsuarioFRM_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        If (e.KeyCode = Keys.Escape) Then
            Me.Close()
        End If
    End Sub
End Class